//
//  YHEmotionKeyboardView.swift
//  EmotionKeyboard
//
//  Created by 昱含 on 16/5/30.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit
import SnapKit
private let keyboardViewHeight: CGFloat = 220
private let emoticonCellId = "emoticonCellId"
private let toolBarHeight: CGFloat = 37
class YHEmotionKeyboardView: UIView {
    
    var packages = YHEmotionManager.sharedEmotion.packages
    
    override init(frame: CGRect) {
        let rect = CGRect(x: 0, y: 0, width: 0, height: keyboardViewHeight)
        super.init(frame: rect)
        setupUI()
       
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor.yellowColor()
        addSubview(toolBar)
        addSubview(collectionView)
        addSubview(pageControl)
        toolBar.snp_makeConstraints { (make) in
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(toolBarHeight)
        }
        
        collectionView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }
        
        pageControl.snp_makeConstraints { (make) in
            make.left.right.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top).offset(-5)
        }
        
        toolBar.selectedBtnCallBack = { (index: Int) -> () in
            
            print(index)
            let indexPath = NSIndexPath(forItem: 0, inSection: index)
            self.collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
             self.updatePageControl(indexPath)
        }
        
//        dispatch_async(dispatch_get_main_queue()) { 
//            let indexPath = NSIndexPath(forItem: 0, inSection: 0)
//            self.updatePageControl(indexPath)
//        }
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置初始的pageControl的信息
        let indexPath = NSIndexPath(forItem: 0, inSection: 0)
        updatePageControl(indexPath)
    }
    
    
//    private lazy var collectionView: UICollectionView = {
//        
//    }()
//    
    private lazy var toolBar: YHEmotionToolBar = YHEmotionToolBar()
    
    private lazy var pageControl: UIPageControl = {
        
        let p = UIPageControl()
        
        p.numberOfPages = 6
        
        p.currentPage = 4
        
        p.userInteractionEnabled = false
        
        p.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "_pageImage")
        
        p.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        
        return p
    }()
    
    private lazy var collectionView: UICollectionView = {
  
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .Horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize(width: UIScreen.mainScreen().bounds.width, height: keyboardViewHeight - toolBarHeight)
        let cv = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        cv.registerClass(YHEmotionCell.self, forCellWithReuseIdentifier: emoticonCellId)
        //设置分页滚动
        cv.pagingEnabled = true
        cv.dataSource = self
        cv.delegate = self
        cv.pagingEnabled = true
        cv.bounces = false
        return cv
    }()
    
    
}


extension YHEmotionKeyboardView: UICollectionViewDataSource,UICollectionViewDelegate{
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return packages.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return packages[section].sectionEmotion.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(emoticonCellId, forIndexPath: indexPath) as! YHEmotionCell
//        cell.backgroundColor = randomColor()
//        cell.indexPath = indexPath
        cell.emotions = packages[indexPath.section].sectionEmotion[indexPath.item]
        return cell
    }
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
//        let indexPaths = collectionView.indexPathsForVisibleItems()
//        for indexPath in indexPaths {
//            toolBar.setSelectBtn(indexPath.section)
//        }
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        var center = collectionView.center
        center.x = center.x + collectionView.contentOffset.x
        let indexPaths = collectionView.indexPathsForVisibleItems()
        for indexPath in indexPaths {
            let cell = collectionView.cellForItemAtIndexPath(indexPath)
            if cell!.frame.contains(center) {
                
                updatePageControl(indexPath)
                toolBar.setSelectBtn(indexPath.section)
            }
        }
    }
    
    private func updatePageControl(indexPath: NSIndexPath) {
        pageControl.numberOfPages = packages[indexPath.section].sectionEmotion.count
        pageControl.currentPage = indexPath.item
    
    }
}
